package com.huawei.hms.framework.network.restclient;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.Converter.Factory;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import com.huawei.hms.framework.network.restclient.hwhttp.ResponseBody;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

final class BuiltInConverters extends Factory {
    private static final String TAG = "BuiltInConverters";

    static final class RequestBodyConverter implements Converter<RequestBody, RequestBody> {
        static final RequestBodyConverter INSTANCE = new RequestBodyConverter();

        RequestBodyConverter() {
        }

        public RequestBody convert(RequestBody requestBody) {
            return requestBody;
        }
    }

    static final class ResponseBodyConverter implements Converter<ResponseBody, ResponseBody> {
        static final ResponseBodyConverter INSTANCE = new ResponseBodyConverter();

        ResponseBodyConverter() {
        }

        public ResponseBody convert(ResponseBody responseBody) {
            return responseBody;
        }
    }

    static final class ToStringConverter implements Converter<Object, String> {
        static final ToStringConverter INSTANCE = new ToStringConverter();

        ToStringConverter() {
        }

        public String convert(Object obj) {
            return obj.toString();
        }
    }

    static final class VoidResponseBodyConverter implements Converter<ResponseBody, Void> {
        static final VoidResponseBodyConverter INSTANCE = new VoidResponseBodyConverter();

        VoidResponseBodyConverter() {
        }

        public Void convert(ResponseBody responseBody) {
            try {
                responseBody.close();
            } catch (Throwable e) {
                Logger.w(BuiltInConverters.TAG, "", e);
            }
            return null;
        }
    }

    BuiltInConverters() {
    }

    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotationArr, RestClient restClient) {
        if (type == ResponseBody.class) {
            return ResponseBodyConverter.INSTANCE;
        }
        if (type == Void.class) {
            return VoidResponseBodyConverter.INSTANCE;
        }
        return null;
    }

    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotationArr, Annotation[] annotationArr2, RestClient restClient) {
        if (RequestBody.class.isAssignableFrom(Utils.getRawType(type))) {
            return RequestBodyConverter.INSTANCE;
        }
        return null;
    }
}
